package com.mrd.gtimpframe.service.impl;

import com.mrd.exception.QueryException;
import com.mrd.gtimp.base.entity.Module;
import com.mrd.gtimp.base.entity.Role;
import com.mrd.gtimp.base.service.RoleBiz;
import com.mrd.gtimp.business.entity.CompanyQp;
import com.mrd.gtimp.business.entity.GoodsCode;
import com.mrd.gtimp.business.service.CompanyQpBiz;
import com.mrd.gtimp.business.service.GoodsCodeBiz;
import com.mrd.gtimpframe.service.RedisDictionaryService;
import com.mrd.redis.JedisClientClusterImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dusy on 2017/7/13.
 */
@Service
public class RedisDictionaryServiceImpl implements RedisDictionaryService{

    @Autowired
    private GoodsCodeBiz goodsCodeBiz;
    @Autowired
    private JedisClientClusterImpl jedisClientCluster;
    @Autowired
    private CompanyQpBiz companyQpBiz;
    @Autowired
    private RoleBiz roleBiz;


    /**
     * 更新GoodsCode到redis
     */
    public String updateGoodsCodeRedies (){
        List<GoodsCode> goodsCodeList = null;
        try {
            goodsCodeList = goodsCodeBiz.findAllEntity();
        } catch (QueryException e) {
            e.printStackTrace();
            return "查找数据库出错" + e.getMessage();
        }
        jedisClientCluster.delete("AllGoodsCode");
        if (goodsCodeList != null && goodsCodeList.size() > 0) {
            jedisClientCluster.leftPush("AllGoodsCode",goodsCodeList);
        }
        return "更新完成";
    }

    @Override
    public List<Module> getModuleByRoleId(String roleId) {
        if (roleId == null) {
            return null;
        }
        List<Module> moduleList = null;
        moduleList = jedisClientCluster.leftPop("RM"+roleId,Module.class);
        if (moduleList == null || moduleList.size() == 0){
            try {
                moduleList = roleBiz.findRoleModule(roleId,null);
                jedisClientCluster.delete("RM"+roleId);
                jedisClientCluster.leftPush("RM"+roleId,moduleList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return moduleList;
    }

    /**
     * 更新CompanyQp到redis
     */
    public String updateCompanyQpRedis(){
        List<CompanyQp> companyQpList = null;
        try {
            companyQpList = companyQpBiz.findAllEntity();
        } catch (QueryException e) {
            e.printStackTrace();
            return "查找数据库出错" + e.getMessage();
        }
        jedisClientCluster.delete("AllCompanyQp");
        if (companyQpList != null && companyQpList.size() != 0) {
            jedisClientCluster.leftPush("AllCompanyQp",companyQpList);
        }
        return "更新完成";
    }
   /**
     * 更新Module到redis
    * @param roleId 角色id,null则更新全部
    * @return 更新结果
     */
    public String updateRoleModuleRedis(String roleId){
        List<Role> roleList = null;
        List<Module> moduleList = null;
        if (roleId == null) {
            try {
                roleList = roleBiz.findAllEntity();
            } catch (QueryException e) {
                e.printStackTrace();
                return "查找数据库出错" + e.getMessage();
            }
        }else{
            roleList = new ArrayList<Role>();
            Role role = new Role();
            role.setId(roleId);
            roleList.add(role);
        }
        if (roleList == null) {
            return "系统还没有创建角色";
        }
        for (Role role : roleList) {
            try {
                moduleList = roleBiz.findRoleModule(role.getId(),null);
            } catch (Exception e) {
                e.printStackTrace();
                return "查找数据库出错" + e.getMessage();
            }
            jedisClientCluster.delete("RM"+role.getId());
            if (moduleList != null && moduleList.size() != 0) {
                jedisClientCluster.leftPush("RM"+role.getId(),moduleList);
            }
        }
        return "更新完成";
    }

}
